package com.framework.service.system.impl;

import com.framework.common.model.response.R;
import com.framework.common.util.other.NumeralUtil;
import com.framework.mapper.system.SystemLogMapper;
import com.framework.model.system.SystemLog;
import com.framework.model.system.SystemUser;
import com.framework.model.system.vo.SystemUserVo;
import com.framework.service.base.BaseService;
import com.framework.service.system.SystemLogService;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.service.system.impl.SystemLogServiceImpl
 * @description 系统管理操作日志业务接口实现类
 * @datetime 2024-06-19 09:34:28
 */
@Service("systemLogServiceImpl")
public class SystemLogServiceImpl extends BaseService implements SystemLogService {
    private Logger log = LoggerFactory.getLogger(SystemLogServiceImpl.class);
    @Autowired
    private SystemLogMapper systemLogMapper;

    /**
     * @param id 1 编号
     * @return com.framework.model.system.SystemLog
     * @title 根据编号查询系统管理操作日志
     * @description 根据编号查询系统管理操作日志
     * @author 龘鵺
     * @datetime 2024-06-19 09:34:28
     * @version 1.0
     */
    @Override
    public SystemLog getByIdParam(Long id) {
        SystemLog row = new SystemLog();
        row.setId(id);
        SystemLog p = systemLogMapper.selectByParam(row);
        if (p == null) {
            return new SystemLog();
        }
        return p;
    }

    /**
     * @param row 1 系统管理操作日志类
     * @return com.framework.common.model.response.R
     * @title 新增系统管理操作日志
     * @description 新增系统管理操作日志
     * @author 龘鵺
     * @datetime 2024-06-19 09:34:28
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> save(SystemLog row) {
        SystemUser systemUser = getUser();
        Long userId = systemUser.getId();
        Date date = new Date();


        row.setOperaterId(userId);
        row.setOperaterTime(date);
        systemLogMapper.insertSelective(row);
        return R.ok();
    }

    /**
     * @param row 1 系统管理操作日志类
     * @return com.framework.common.model.response.R
     * @title 编辑系统管理操作日志
     * @description 编辑系统管理操作日志
     * @author 龘鵺
     * @datetime 2024-06-19 09:34:28
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> edit(SystemLog row) {
        SystemLog queryParam = systemLogMapper.selectByPrimaryKey(row.getId());
        if (queryParam == null) {
            return R.fail("编辑信息不存在!");
        }
        SystemUser systemUser = getUser();
        Long userId = systemUser.getId();
        Date date = new Date();
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        systemLogMapper.updateByPrimaryKeySelective(row);
        return R.ok();
    }

    /**
     * @param id 1 编号
     * @return com.framework.common.model.response.R
     * @title 删除系统管理操作日志
     * @description 删除系统管理操作日志
     * @author 龘鵺
     * @datetime 2024-06-19 09:34:28
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> delete(Long id) {
        SystemLog row = new SystemLog();
        SystemUser systemUser = getUser();
        Long userId = systemUser.getId();
        Date date = new Date();
        row.setId(id);
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        systemLogMapper.delete(row);
        return R.ok();
    }

    /**
     * @param idList 1 编号集合
     * @return com.framework.common.model.response.R
     * @title 批量删除系统管理操作日志
     * @description 批量删除系统管理操作日志
     * @author 龘鵺
     * @datetime 2024-06-19 09:34:28
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> batchDeleteList(List<Long> idList) {
        SystemLog row = new SystemLog();
        SystemUser systemUser = getUser();
        Long userId = systemUser.getId();
        Date date = new Date();
        row.setIdList(idList);
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        //systemLogMapper.deleteList(row);
        return R.ok();
    }

    /**
     * @param row 1 系统管理操作日志类
     * @return java.util.List<com.framework.model.system.SystemLog>
     * @title 查询系统管理操作日志集合
     * @description 查询系统管理操作日志集合
     * @author 龘鵺
     * @datetime 2024-06-19 09:34:28
     * @version 1.0
     */
    @Override
    public List<SystemLog> findByList(SystemLog row) {
        List<SystemLog> list = systemLogMapper.findByList(row);
        return list;
    }

    /**
     * @param row 1 系统管理操作日志类
     * @return com.framework.common.model.response.R
     * @title 分页查询系统管理操作日志列表
     * @description 分页查询系统管理操作日志列表
     * @author 龘鵺
     * @datetime 2024-06-19 09:34:28
     * @version 1.0
     */
    @Override
    public R<?> findParamPageList(SystemLog row) {
        int count = systemLogMapper.findPageListCount(row);
        if (count == NumeralUtil.POSITIVE_ZERO) {
            return R.ok();
        }
        List<SystemLog> list = systemLogMapper.findPageList(row);
        return R.ok(list, count);
    }

    /**
     * @param user            1 用户实体类
     * @param ip              2 登录IP
     * @param operaterTitle   3 操作标题
     * @param operaterContent 4 操作内容
     * @title 用户操作日志添加
     * @description 用户操作日志添加
     * @author 龘鵺
     * @datetime 2024-06-19 10:50:37
     */
    @Override
    @Async
    public void loginInfo(SystemUserVo user, String ip, String operaterTitle, String operaterContent) {
        if (user == null) {
            return;
        }
        Date date = new Date();
        SystemLog row = new SystemLog();
        row.setOperaterTime(date);
        row.setOperaterId(user.getId());
        row.setIp(ip);
        row.setAccount(user.getAccount());
        row.setOperaterTitle(operaterTitle);
        row.setOperaterContent(operaterContent);
        systemLogMapper.insertSelective(row);
    }

}